package com.mo.axion.service.api.impl.config;

import com.mo.axion.common.pipeline.ProcessController;
import com.mo.axion.common.pipeline.ProcessTemplate;
import com.mo.axion.enums.BusinessCode;
import com.mo.axion.service.api.impl.action.send.SendAfterCheckAction;
import com.mo.axion.service.api.impl.action.send.SendAssembleAction;
import com.mo.axion.service.api.impl.action.send.SendMqAction;
import com.mo.axion.service.api.impl.action.send.SendPreCheckAction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Mo
 * @createTime 2024/2/1 23:21
 * @description
 */
@Slf4j
@Configuration
public class PipelineConfig {
    @Autowired
    private SendPreCheckAction sendPreCheckAction;

    @Autowired
    private SendAssembleAction sendAssembleAction;

    @Autowired
    private SendAfterCheckAction sendAfterCheckAction;

    @Autowired
    private SendMqAction sendMqAction;

    /**
     * 普通消息发送请求执行流程
     * 1. 入参校验
     * 2. 参数组装
     * 3. 后置参数校验
     * 4. 发送消息至MQ
     * @return 发送模板实例
     */
    @Bean("commonSendTemplate")
    public ProcessTemplate commonSendTemplate() {
        ProcessTemplate processTemplate = new ProcessTemplate();
        processTemplate.setProcessList(Arrays.asList(
                sendPreCheckAction,
                sendAssembleAction,
                sendAfterCheckAction,
                sendMqAction
        ));
        log.info("Initialized PipelineAction for pipelineConfig:" + processTemplate.getProcessList());
        return processTemplate;
    }

    @Bean("apiProcessController")
    public ProcessController apiProcessController() {
        ProcessController processController = new ProcessController();
        Map<String, ProcessTemplate> templateConfig = new HashMap<>(4);
        templateConfig.put(BusinessCode.COMMON_SEND.getCode(), commonSendTemplate());
//        templateConfig.put(BusinessCode.RECALL.getCode(),
        processController.setTemplateConfig(templateConfig);
        return processController;
    }
}
